package com.example.demo.http.controllers.admin;

import com.example.demo.models.user.User;
import com.example.demo.services.system.ResultService;
import gaarason.database.appointment.Paginate;
import gaarason.database.contract.eloquent.Builder;
import gaarason.database.contract.eloquent.Record;
import gaarason.database.exception.EntityNotFoundException;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author lucifer
 * @since 2024-01-31-11:17
 */
@RestController
@RequestMapping("/admin/user/")
public class UserController {

    @Resource
    User.Model userModel;

    @GetMapping("/")
    ResultService.Result index(@RequestParam Map<String, String> params) {
        //分页单独处理
        String page = params.get("page");
        if (Objects.isNull(page)) {
            //如果没有页数，默认为第一页
            page = "1";
        }
        //生成查询器
        Builder<User, Long> builder = userModel.newQuery();
        //放入参数
        params.forEach((key, value) -> {
            if (!Objects.isNull(key) && !key.equals("page")) {
                //要去掉page这个参数，如果还要去掉其他就直接在这里加
                if (!Objects.isNull(value)) {
                    builder.where(key, value);
                }
            }
        });

        //返回结果
        Paginate<User> paginate = builder.paginate(page, 15);
        //对结果进行处理
        Map<String, Object> data = new HashMap<>();
        data.put("currentPage", paginate.getCurrentPage());
        data.put("lastPage", paginate.getLastPage());
        data.put("total", paginate.getTotal());
        data.put("perPage", paginate.getPerPage());
        data.put("data", paginate.getItemList());
        ResultService.Result result = ResultService.getNormalResult();
        result.data = data;
        return result;
    }

    //查询单个用户
    @GetMapping("/item")
    ResultService.Result item(@RequestParam Map<String, String> params) {
        //返回值
        ResultService.Result result = ResultService.getNormalResult();
        Record<User, Long> user;
        if (Objects.isNull(params.get("id"))) {
            //管理员不存在
            result.code = ResultService.NOT_FOUND;
            result.msg = "用户不存在";
            return result;
        } else {
            try {
                user = userModel.newQuery().findOrFail(params.get("id"));
            } catch (EntityNotFoundException e) {
                //找不到活动
                result.code = ResultService.NOT_FOUND;
                result.msg = "用户不存在";
                return result;
            }
            //正常返回结果
            result.data = user.getEntity();
        }
        return result;
    }

    @PostMapping("/change")
    ResultService.Result change(@RequestParam Map<String, String> params) {
        //返回值
        ResultService.Result result = ResultService.getNormalResult();
        Record<User, Long> user;
        if (Objects.isNull(params.get("id"))) {
            //新建
            user = userModel.newRecord();
        } else {
            //更新
            try {
                user = userModel.newQuery().findOrFail(params.get("id"));
            } catch (EntityNotFoundException e) {
                //找不到就报错
                result.code = ResultService.NOT_FOUND;
                result.msg = "id not found";
                return result;
            }
        }
        //填充对应参数
        user.getEntity().setNumber(params.get("number"));
        user.getEntity().setName(params.get("name"));
        user.getEntity().setGender(params.get("gender"));
        user.getEntity().setGrade(params.get("grade"));
        user.getEntity().setCollege(params.get("college"));
        user.getEntity().setMajor(params.get("major"));
        user.getEntity().setClazz(params.get("clazz"));
        user.save();
        return result;
    }

    @GetMapping("/delete")
    ResultService.Result delete(@RequestParam Map<String, String> params) {
        //返回值
        ResultService.Result result = ResultService.getNormalResult();
        if (!Objects.isNull(params.get("id"))) {
            //删除
            try {
                userModel.newQuery().findOrFail(params.get("id")).delete();
            } catch (EntityNotFoundException e) {
                //找不到就报错
                result.code = ResultService.NOT_FOUND;
                result.msg = "id not found";
                return result;
            }
        }
        return result;
    }
}
